package com.yonyou.isv.demo.openapi.base.module.isv.event;

import com.alibaba.fastjson.JSON;
import com.yonyou.isv.demo.openapi.base.module.auth.isv.ISVCryptoService;
import com.yonyou.isv.demo.openapi.base.module.isv.event.pojo.ISVEvent;
import com.yonyou.isv.demo.openapi.base.network.cryptor.EncryptionHolder;
import com.yonyou.isv.demo.openapi.base.properties.UCFCoreProperties;
import com.yonyou.isv.demo.openapi.event.IsvEventExtendType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 本类主要用于
 *
 * @author liuhaoi
 * @since Created At 2020/5/25 11:03 上午
 */
@Slf4j
@ResponseBody
@RequiredArgsConstructor
@RequestMapping("/rest/v1/openapi/isv/event")
public class ISVEventListenerController {


    
    private final ISVCryptoService cryptoService;

    private final ISVReceivedEventDispatcher dispatcher;

    @Resource
    UCFCoreProperties ucfCoreProperties;

    @PostMapping
    public String onEvent(@RequestBody EncryptionHolder holder) {
        log.info(JSON.toJSONString(holder));
        String decrypt = cryptoService.decrypt(holder);
        ISVEvent event = JSON.parseObject(decrypt, ISVEvent.class);
        log.info("进入回调地址，解析密文--》"+event);

        dispatcher.dispatch(event, decrypt, holder);
        return event.getType().equals(IsvEventExtendType.SUITE_AUTH)?ucfCoreProperties.getOrderDealWay():"success";
    }
}
